package com.skywolf.chem.manager.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.skywolf.chem.article.entity.ContestNotice;
import com.skywolf.chem.article.entity.LiveInfo;
import com.skywolf.chem.article.pojo.ContestQueryParam;
import com.skywolf.chem.article.pojo.LiveQueryParam;
import com.skywolf.chem.article.service.IContestNoticeService;
import com.skywolf.chem.article.service.ILiveInfoService;
import com.skywolf.chem.common.sysconfig.pojo.vo.PageVO;
import com.skywolf.chem.common.sysconfig.response.RespData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Created by pei_pei on 2020/6/29.
 */
@RestController
@RequestMapping("/manager/live")
public class LiveManagerController {

    @Autowired
    ILiveInfoService liveInfoService;

    @Autowired
    IContestNoticeService contestNoticeService;


    public static String getRandomNumber() {
        double min = 0.1;//最小值
        double max = 5.9;//总和
        int scl =  1;//小数最大位数
        int pow = (int) Math.pow(10, scl);//指定小数位
        double one = Math.floor((Math.random() * (max - min) + min) * pow) / pow;
        return one+"k";
    }


    @ApiOperation(value = "创建直播",notes = "创建直播")
    @RequestMapping(value = "/addlive",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<LiveInfo> addliveInfo(@RequestBody @Validated LiveInfo liveInfo) {
        String num = getRandomNumber();
        liveInfo.setViewCount(num);
        liveInfoService.save(liveInfo);
        return RespData.success("success");
    }

    @ApiOperation(value = "创建赛事",notes = "创建直播")
    @RequestMapping(value = "/addliveNotice",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<LiveInfo> addliveNotice(@RequestBody @Validated ContestNotice notice) {
        contestNoticeService.save(notice);
        return RespData.success("success");
    }


    @ApiOperation(value = "修改直播信息",notes = "修改直播信息")
    @RequestMapping(value = "/updateLive",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<LiveInfo> updateLiveInfo(@RequestBody @Validated LiveInfo liveInfo) {
        liveInfo.setCreatedTime(null);
        liveInfoService.updateById(liveInfo);
        return RespData.success("success");
    }


    @ApiOperation(value = "删除直播|回播",notes = "删除直播|回播")
    @RequestMapping(value = "/delLive",method = {RequestMethod.GET},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> delLive(@RequestParam(value = "id",required = true) Long id) {
        liveInfoService.removeById(id);
        return RespData.success("success");
    }


    @ApiOperation(value = "直播更新为回播",notes = "直播更新为回播")
    @RequestMapping(value = "/reLive",method = {RequestMethod.GET},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> reLive(@RequestParam(value = "id",required = true) Long id) {
        LiveInfo info = new LiveInfo();
        info.setId(id);
        info.setVideoType(1);
        liveInfoService.updateById(info);
        return RespData.success("success");
    }


    @ApiOperation(value = "直播列表",notes = "直播列表")
    @RequestMapping(value = "/getLiveList",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<LiveInfo> getLiveList(@RequestBody @Validated LiveQueryParam queryParam) {
//        queryParam.setVideoType(0);
        IPage<LiveInfo> page = liveInfoService.livePageList(queryParam);
        PageVO<LiveInfo> pageVO = new PageVO<>(page);
        return RespData.success(pageVO);
    }

    @ApiOperation(value = "回播列表",notes = "回播列表")
    @RequestMapping(value = "/getReLiveList",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<LiveInfo> getReLiveList(@RequestBody @Validated LiveQueryParam queryParam) {
        queryParam.setVideoType(1);
        IPage<LiveInfo> page = liveInfoService.reLivePageList(queryParam);
        PageVO<LiveInfo> pageVO = new PageVO<>(page);
        return RespData.success(pageVO);
    }

    @ApiOperation(value = "赛事预告列表",notes = "赛事预告列表")
    @RequestMapping(value = "/getLiveNotice",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<ContestNotice> getLiveNotice(@RequestBody @Validated ContestQueryParam queryParam) {
        IPage<ContestNotice> page = contestNoticeService.liveNoticePageList(queryParam);
        PageVO<ContestNotice> pageVO = new PageVO<>(page);
        return RespData.success(pageVO);
    }

    @ApiOperation(value = "修改赛事预告信息",notes = "修改赛事预告信息")
    @RequestMapping(value = "/updateNotice",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<LiveInfo> updateLiveInfo(@RequestBody @Validated ContestNotice notice) {
        notice.setCreatedTime(null);
        contestNoticeService.updateById(notice);
        return RespData.success("success");
    }


    @ApiOperation(value = "删除赛事预告",notes = "删除赛事预告")
    @RequestMapping(value = "/delContestNotice",method = {RequestMethod.GET},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> delContestNotice(@RequestParam(value = "id",required = true) Long id) {
        contestNoticeService.removeById(id);
        return RespData.success("success");
    }

    @ApiOperation(value = "直播详情",notes = "直播详情")
    @RequestMapping(value = "/liveDetail",method = {RequestMethod.GET},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<LiveInfo> liveDetail(@RequestParam(value = "id",required = true) Long id) {
        LiveInfo info = liveInfoService.getById(id);
        return RespData.success(info);
    }

    @ApiOperation(value = "赛事预告详情",notes = "赛事预告详情")
    @RequestMapping(value = "/noticeDetail",method = {RequestMethod.GET},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<ContestNotice> noticeDetail(@RequestParam(value = "id",required = true) Long id) {
        ContestNotice info = contestNoticeService.getById(id);
        return RespData.success(info);
    }
}
